<template>
	<div>
		<commons-alert @alert-dismissed="alertDismissed" :variant="alert.variant" :dismissCountDown="alert.dismissCountDown">{{alert.message}}</commons-alert>
		<page-title>{{title}}</page-title>
		<b-form @submit="onSubmit" @reset="onReset" class="m-1">
			<slot></slot>
			<b-button class="float-right m-1" v-show="!readonly" :disabled="readonly" type="reset" variant="danger">重置</b-button>
			<b-button class="float-right m-1" v-show="!readonly" :disabled="readonly" type="submit" variant="primary">保存</b-button>
			<b-button class="float-right m-1" @click="viewBack" variant="info">返回</b-button>
		</b-form>
	</div>
</template>

<script>
	import PageTitle from './ContentPageTitle.vue'
	import axios from 'axios'
	import gl from './global_variable.js'
	import CommonsAlert from './Alert.vue'

	export default {
		name: "my-form",
		components: {
			PageTitle,
			CommonsAlert
		},
		props: {
			title: {
				type: String,
				required: true
			},
			readonly: {
				type: Boolean,
				default: false
			}
		},
		data: function() {
			return {
				alert: {
					message: "",
					dismissCountDown: 0,
					variant: "success"
				}
			}
		},
		created: function() {
			this.$emit("form-created")
		},
		methods: {
			onSubmit: function(evt) {
				this.$emit("form-submit")
			},
			onReset: function(evt) {
				this.$emit("form-reset")
			},
			alertDismissed: function() {
				this.alert.dismissCountDown = 0
				this.$emit("form-alert-dismissed-" + this.alert.variant)
			},
			viewBack: function() {
				this.$emit("form-view-back")
			},
			onSaveSuccess: function(message) {
				this.alert.message = message || "保存成功"
				this.alert.variant = "success"
				this.alert.dismissCountDown = 5
				this.$emit("form-save-success")
			},
			onSaveError: function(message) {
				this.alert.message = message || "保存失败"
				this.alert.variant = "warning"
				this.alert.dismissCountDown = 5
				this.$emit("form-save-error")
			},
			onQueryError: function(message) {
				this.alert.message = message || "查询数据失败"
				this.alert.variant = "warning"
				this.alert.dismissCountDown = 5
				this.$emit("form-query-error")
			}
		}
	}
</script>

<style>
</style>
